package org.jeecg.modules.demo.msgtemplatelist.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.demo.msgtemplatelist.entity.MsgTemplate;
import org.jeecg.modules.demo.msgtemplatelist.mapper.MsgTemplateMapper;
import org.jeecg.modules.demo.msgtemplatelist.service.IMsgTemplateService;
import org.jeecg.modules.demo.storeinfo.entity.StoreInfo;
import org.jeecg.modules.demo.storeinfo.service.IStoreInfoService;
import org.jeecg.modules.util.HttpUtils;
import org.jeecg.modules.util.WxUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 消息模板列表
 * @Author: jeecg-boot
 * @Date: 2022-07-10
 * @Version: V1.0
 */
@Service
public class MsgTemplateServiceImpl extends ServiceImpl<MsgTemplateMapper, MsgTemplate> implements IMsgTemplateService {
    @Autowired
    private MsgTemplateMapper msgTemplateMapper;
    @Autowired
    private IStoreInfoService storeInfoService;

    public MsgTemplate getByTitle(String title, String storeId) {
        MsgTemplate msgTemplate = msgTemplateMapper.getByTitle(title, storeId);
        if (msgTemplate == null) {
            deleteByStoreId(storeId);
            StoreInfo store = storeInfoService.getById(storeId);
            String gzhAppId = store.getGzhAppId();
            String gzhAppSecret = store.getGzhAppSecret();
            String url1 = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + gzhAppId + "&secret=" + gzhAppSecret;
            String result1 = HttpUtils.sendGet(url1);
            JSONObject resultJson1 = JSON.parseObject(result1);
            String errcode = resultJson1.getString("errcode");
            if (errcode != null && !"0".equals(errcode)) {
                return null;
            } else {
                String accessToken = resultJson1.getString("access_token");
                Map<String, Object> map = WxUtils.getTemplateList(accessToken);
                System.out.println(map);
                List<Map> listMsgTemplateEntity = (List<Map>) JSON.parse(map.get("template_list").toString());
                for (int i = 0; i < listMsgTemplateEntity.size(); i++) {
                    msgTemplate = new MsgTemplate();
                    msgTemplate.setTemplateId(listMsgTemplateEntity.get(i).get("template_id").toString());
                    msgTemplate.setTitle(listMsgTemplateEntity.get(i).get("title").toString());
                    msgTemplate.setPrimaryIndustry(listMsgTemplateEntity.get(i).get("primary_industry").toString());
                    msgTemplate.setDeputyIndustry(listMsgTemplateEntity.get(i).get("deputy_industry").toString());
                    msgTemplate.setContent(listMsgTemplateEntity.get(i).get("content").toString());
                    msgTemplate.setExample(listMsgTemplateEntity.get(i).get("example").toString());
                    msgTemplate.setStoreId(storeId);
                    msgTemplate.setCreateBy("sys");
                    msgTemplate.setCreateTime(new Date());
                    System.out.println("msgTemplate:" + msgTemplate);
                    save(msgTemplate);
                }
            }
        }
        return msgTemplate;
    }

    public void deleteByStoreId(String storeId) {
        msgTemplateMapper.deleteByStoreId(storeId);
    }
}
